﻿<form method="post">

    @Html.AntiForgeryToken()

    <div class="querybar">
        <table cellpadding="0" cellspacing="1" border="0" width="100%">
            <tr>
                <td>
                    名称：<input type="text" class="mytext" style="width:190px;" id="Name" name="Name" value="" />
                    <input type="button" name="Search" onclick="loadData();" value="&nbsp;&nbsp;查&nbsp;询&nbsp;&nbsp;" class="mybutton" />
                    <input type="button" onclick="edit(); return false;" value="添加地区" class="mybutton" />
                    <input type="button" onclick="return del();" name="DeleteBut" value="删除所选" class="mybutton" />
                </td>
            </tr>
        </table>
    </div>
    <table id="listtable"></table>

</form>

<script>

    var appid = "wjj";
    var iframeid = "wjj";
    var dialog = top.mainDialog || new RoadUI.Window();
    var rows = @Html.Raw(ViewData["json"]);

    function loadData() {
        var name = $.trim($("#Name").val()).toLowerCase();
        $("#listtable").jqGrid('clearGridData');
        for (var i = 0; i < rows.length; i++) {
            if (name.length == 0 || rows[i].Name.toLowerCase().indexOf(name) >= 0) {
                $("#listtable").jqGrid('addRowData', rows[i].Id, rows[i]);
            }
        }
    }

    function edit(id) {
        dialog.open({
            id: "window_" + appid.replaceAll('-', ''),
            title: "编辑按钮",
            width: 700,
            height: 460,
            url: 'Edit?buttonid=' + (id || "") + '&appid=' + appid + "&tabid=" + iframeid,
            opener: window,
            openerid: iframeid
        });
    }

    function del() {
        var rowIds = $("#listtable").jqGrid('getGridParam', 'selarrrow');
        if (rowIds.length == 0) {
            alert("您没有选择要删除的按钮!");
            return false;
        }
        if (!confirm('您真的要删除所选按钮吗?')) {
            return false;
        }
        $.ajax({
            url: "Delete?appid=" + appid + "&tabid=" + iframeid,
            type: "post",
            data: {
                "ids": rowIds.join(','),
                "__RequestVerificationToken": $("input[name='__RequestVerificationToken']").val()
            },
            success: function (txt) {
                alert(txt);
                window.location = window.location;
            }
        });
        return true;
    } 

    $(function () {
        $("#listtable").jqGrid({
            datatype: "local",
            colNames: ['Id', '名称',''],
            colModel: [
                { name: 'Id', index: 'Id' },
                { name: 'Name', index: 'Name' },
                { name: 'Opation', index: 'Opation', sortable: false, width: 80 },
            ],
            multiselect: true,
            height: $(window).height() - 80,
            rowNum: rows.length,
            width: $(window).width()
        });
        loadData();
    });
</script>